package com.das.mechanic_base.utils;

import android.content.Context;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Build;

import com.das.mechanic_base.R;

/**
 * Created by chensi on 2018/5/9.
 */
public class X3SoundPlayUtils {

    // SoundPool对象
    @Deprecated
    public static SoundPool mSoundPlayer;

    public static X3SoundPlayUtils soundPlayUtils;

    // 上下文
    static Context mContext;

    /**
     * 初始化
     *
     * @param context
     */
    public static X3SoundPlayUtils init(Context context) {
        if (soundPlayUtils == null) {
            soundPlayUtils = new X3SoundPlayUtils();
        }

        if (Build.VERSION.SDK_INT >= 21) {
            AudioAttributes.Builder attrBuilder = new AudioAttributes.Builder();
            attrBuilder.setLegacyStreamType(AudioManager.STREAM_SYSTEM);
            mSoundPlayer = new SoundPool.Builder().setMaxStreams(2)
                           .setAudioAttributes(attrBuilder.build())
                           .build();
        }
        else {
            mSoundPlayer = new SoundPool(5, AudioManager.STREAM_SYSTEM, 0);
        }

        // 初始化声音
        mContext = context;

        try {
            mSoundPlayer.load(mContext,R.raw.x3_new_work,1);//1
            mSoundPlayer.load(mContext,R.raw.x3_del_tag,1);//1
        }catch (Exception e) {
            e.printStackTrace();
        }
        return soundPlayUtils;
    }

    /**
     * 播放声音
     *
     * @param soundID
     */
    public static void play(int soundID) {
        mSoundPlayer.play(soundID, 1, 1, 0, 0, 1);
    }
}
